﻿/*
 * RMS.java
 *
 * Created on 2007年10月22日, 下午 3:43
 *
 */

package org.uranus.core.dataHandler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * Copyright 2007 Hsiang-Yu Wang<br>
 * This file is distributed under the terms of the GNU General Public License<br>
 *<br>
 *<pre>
 * --------------------License notice--------------------
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,
 * visit: http://www.gnu.org/licenses/gpl.txt
 * --------------------License notice--------------------</pre>
 *
 * @author Hsiang-Yu Wang
 */
public abstract class RMS {
    public String rmsName;
    
    public void save(String rmsName){
        try {
            if(findRecord(rmsName)){
                RecordStore.deleteRecordStore(rmsName);
            }
            
            RecordStore rs = RecordStore.openRecordStore(rmsName, true);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(out);
            
            write(dout);
            dout.close();
            
            rs.addRecord(out.toByteArray(), 0 , out.toByteArray().length);
            
            rs.closeRecordStore();
            out.close();
            
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public boolean findRecord(String rmsName){
        String[] list = RecordStore.listRecordStores();
        if(list != null){
            for(int i = 0 ; i < list.length ; i++){
                if(list[i].equals(rmsName)){
                    return true;
                }
            }
        }
        return false;
    }
    
    public void deleteRecord(){
        try {
            RecordStore.deleteRecordStore(rmsName);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
    
    
    public void load(String rmsName){
        try {
            RecordStore rs = RecordStore.openRecordStore(rmsName, false);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            while(re.hasNextElement()){
                DataInputStream in = new DataInputStream(new ByteArrayInputStream(re.nextRecord()));
                read(in);
                in.close();
                
            }
            
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (InvalidRecordIDException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public static void clearAllRecord(){
        try {
            String[] list = RecordStore.listRecordStores();
            for(int i = 0 ; i < list.length ; i++){
                RecordStore.deleteRecordStore(list[i]);
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
    abstract public void write(DataOutputStream out) throws IOException;
    abstract public void read(DataInputStream in) throws IOException;
    abstract public void save();
}
